package cn.fl.system.controller;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysUsrAppealLogQC;
import cmm.user.facade.system.constant.SysUsrAppealLogConstant;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysUsrAppealLogService;
import cn.fl.system.vo.SysUsrAppealLogVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户申诉记录表 控制层
 */
@Controller
@RequestMapping("/system/sysusrappeallog")
public class SysUsrAppealLogController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysUsrAppealLogController.class);

	/**
	 * SysUsrAppealLog 服务层BEAN
	 */
	@Autowired
	private ISysUsrAppealLogService sysUsrAppealLogService;

	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("system/sysUsrAppeal/sysUsrAppealPage");

		String resultCd = SysUsrAppealLogConstant.USR_APPEAL_DICT_CODE;
		String frontCd = DictionaryConstant.CODE_MGT_SYS_DS_CD;

		String sysCd = WebBaseConstant.SYS_SYS_CD;
		String orgCd = WebBaseConstant.SYS_P_ORG_CD;
		List<String> codeGrpCdList = new ArrayList<>();
		codeGrpCdList.add(frontCd);
		codeGrpCdList.add(resultCd);
		Map<String, List<KeyValueVO>> multiKvListMap = this.sysDictionaryApiService.getMultiKvList(codeGrpCdList,sysCd, orgCd);

		//状态
		List<KeyValueVO> resultCdList = multiKvListMap.get(resultCd);
		if (resultCdList != null) {
			view.addObject("resultList", resultCdList);
		}

		List<KeyValueVO> frontCdList = multiKvListMap.get(frontCd);
		if (frontCdList != null) {
			view.addObject("frontCdList", frontCdList);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());


		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchFrontCd = request.getParameter("searchFrontCd");
		String searchResult = request.getParameter("searchResult");
		String searchUsrNm = request.getParameter("searchUsrNm");
		String searchRlNm = request.getParameter("searchRlNm");

		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysUsrAppealLogQC qc = new SysUsrAppealLogQC();
		if (StringUtils.isNotEmpty(searchFrontCd)) {
			qc.setSysCd(searchFrontCd);
		}
		if (StringUtils.isNotEmpty(searchResult)) {
			qc.setResult(searchResult);
		}
		if (StringUtils.isNotEmpty(searchUsrNm)) {
			qc.setUsrNm(searchUsrNm);
		}
		if (StringUtils.isNotEmpty(searchRlNm)) {
			qc.setRlNm(searchRlNm);
		}
		String buOrgCd=CurrentThreadContext.getCurrentBuOrgCd();
		String orgCd=CurrentThreadContext.getCurrentOrgCd();
		//二手车金融事业部特殊处理
		if(orgCd.contains("000000010024")){
			buOrgCd="000000010024";
		//改装车事业部特殊处理
		}else if(orgCd.contains("000000010025")){
			buOrgCd="000000010025";
		}
		qc.setBuOrgCd(buOrgCd);
		page.setCondition(qc);

		Pager pager = this.sysUsrAppealLogService.searchListPage(page);
		List<SysUsrAppealLogVO> list = (List<SysUsrAppealLogVO>) pager.getResultList();
		ListPageVO<SysUsrAppealLogVO> listPage = new ListPageVO<SysUsrAppealLogVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do")
	public ModelAndView toDetailPage(@ModelAttribute SysUsrAppealLogVO vo, HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", "加载例子详细页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		System.out.println(JSON.toJSONString(vo));
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysUsrAppeal/sysUsrAppealDetail");
		view.addObject("strId", id);

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", " 加载例子详细页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * 得到一条记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}

		try {
			SysUsrAppealLogVO vo = this.sysUsrAppealLogService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysUsrAppeal/sysUsrAppealModify");
		view.addObject("strId", id);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	




	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(SysUsrAppealLogVO vo,HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		try {
			if(null == vo || StringUtils.isEmpty(vo.getId())){
				throw new BaseException("对象未查到");
			}
			if(StringUtils.isEmpty(vo.getResult())){
				throw new BaseException("请输入审批结果");
			}

			SysUsrAppealLogVO sysUsrAppealLogVO=this.sysUsrAppealLogService.selectById(vo.getId());
			if(!SysUsrAppealLogConstant.USR_APPEAL_CODE_0.equals(sysUsrAppealLogVO.getResult())){
				throw new BaseException("当前申诉已处理，无需再次处理");
			}
			vo.setUsrNm(sysUsrAppealLogVO.getUsrNm());
			vo.setSysCd(sysUsrAppealLogVO.getSysCd());
			boolean success = this.sysUsrAppealLogService.modify(vo);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+vo.getId()+"] DATA:" + JSONObject.toJSONString(vo), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+vo.getId()+"] DATA:" + JSONObject.toJSONString(vo), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,id["+vo.getId()+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,id["+vo.getId()+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}



}

